<template>
  <el-dialog class="my-add" :title="$store.state.add.info.id?'编辑':'新增'" :visible.sync="$store.state.add.show" width="30%" :before-close="handleClose" @open="setData">
    <ul>
      <li v-for="(item,index) in modelData" class="add-item">
        <span>{{item.label}}：</span>

        <el-input :key="index" v-if="item.type=='textarea'" type="textarea" :rows="4" :placeholder="'请输入'+item.label" v-model="$store.state.add.info[item.key]"></el-input>

        <p v-else-if="item.type=='label'" class="label-content">{{$store.state.add.info[item.key]}}</p>

        <el-select :key="index" v-else-if="item.type=='select'" v-model="$store.state.add.info[item.key]" placeholder="请选择">
          <el-option v-for="(opt,optIndex) in $store.state.selectData[item.array].data" :key="optIndex" :label="opt.label" :value="opt.value"></el-option>
        </el-select>

        <el-input :key="index" v-else-if="item.type=='input'" type="text" :placeholder="'请输入'+item.label" v-model="$store.state.add.info[item.key]"></el-input>

        <el-upload :key="index" v-else-if="item.type=='upload'" class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
          <img v-if="$store.state.add.info[item.key]" :src="$store.state.add.info[item.key]" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </li>
    </ul>

    <span slot="footer" class="dialog-footer">
      <el-button @click="$store.state.add.show = false">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import addData from "./addData.js";
export default {
  components: {},
  name: "",
  data() {
    return {
      modelData: [] //add数据模板
    };
  },
  methods: {
    setData() {
      const path = _g.getRouterPath(this);
      addData.forEach(ele => {
        if (ele.path === path) {
          this.$set(this, "modelData", ele.data);
          return;
        }
      });
    },
    handleClose() {
      // 关闭前数据重置
      console.log("close");
      this.$store.state.add.info = { id: "" };
      this.$store.state.add.show = false;
    },

    // 上传
    handleAvatarSuccess() {},
    beforeAvatarUpload() {},

    confirm() {
      _g.orderMethods([this.$store.state.add.OK, this.handleClose]);
    }
  },
  created() {}
};
</script>
<style lang="scss" scoped>
.my-confirm {
}
.add-item {
  display: flex !important;
  padding: 0 5%;
  min-height: 40px;
  margin-top: 5px;

  > span {
    display: inline-block;
    font-size: 16px;
    width: 60px;
    line-height: 40px;
  }
  .label-content {
    font-size: 16px;
    width: 50%;
    flex-grow: 1;
    padding: 5px;
    line-height: 30px;
  }
  .avatar-uploader{
    border: 1px dashed #d9d9d9;
  }
  .avatar-uploader .el-upload {
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    background-color: red;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 150px;
    height: 150px;
    line-height: 150px;
    text-align: center;
  }
  .avatar {
    width: 150px;
    height: 150px;
    display: block;
  }
}
</style>